package com.dpiinc.TechPage.user.setting;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.room.RoomAiWriterDatabase;
import com.dpiinc.TechPage.room.bean.UserEntity;
import com.dpiinc.TechPage.user.CommonWebviewActivity;
import com.dpiinc.TechPage.user.VideoWebViewActivity;
import com.dpiinc.TechPage.user.utils.Constant;
import com.dpiinc.TechPage.utils.AppUtil;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Locale;

import androidx.annotation.Nullable;

/**
 * Created by hu489_000 on 2018-06-25.
 */

public class MyInfoActivity extends BaseActivity {
    private static final String TAG = "MyInfoActivity";
    ImageView user_imageView;
    TextView user_nickname;
    TextView tv_userid;
    //    View label_store;
    View label_setting;
    View my_info_setting;
    View feed_back_setting;
    View helper_setting;
    View video_tutorial;
    TextView tv_version;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.my_info_main);
        super.onCreate(savedInstanceState);

//        setTitle(getResources().getString(R.string.back_title));
//        setBackWhite();
//        setstatusBarColor(R.color.color_00bad2);
        user_imageView = findViewById(R.id.user_imageView);
        user_nickname = findViewById(R.id.user_nickname);
        tv_userid = findViewById(R.id.tv_userid);
        tv_version = findViewById(R.id.tv_app_version);


//        label_store = findViewById(R.id.label_store);
//        label_store.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent();
//                intent.setData(Uri.parse(Constant.APP_STORE));
//                intent.setAction(Intent.ACTION_VIEW);
//                startActivity(intent);
//            }
//        });

        label_setting = findViewById(R.id.label_setting);
        label_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MyInfoActivity.this, LabelAvtivity.class));
            }
        });

        my_info_setting = findViewById(R.id.my_info_setting);
        my_info_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MyInfoActivity.this, SettingActivity.class));
            }
        });
        feed_back_setting = findViewById(R.id.feed_back_setting);
        feed_back_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MyInfoActivity.this, FeedBackActivity.class));
            }
        });


        helper_setting = findViewById(R.id.helper_setting);
        helper_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* if ("en".equals(getSharedPreferences("app_language", Context.MODE_PRIVATE).getString("lauType", Locale.getDefault().getLanguage()))) {
                    CommonWebviewActivity.webviewEntrance(MyInfoActivity.this, Constant.APP_HELIPER_EN, "");
                } else {
                    CommonWebviewActivity.webviewEntrance(MyInfoActivity.this, Constant.APP_HELIPER, "");
                }*/
                startActivity(new Intent(MyInfoActivity.this, HelperSetting.class));
            }
        });

        video_tutorial = findViewById(R.id.video_tutorial);
        video_tutorial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("en".equals(getSharedPreferences("app_language", Context.MODE_PRIVATE).getString("lauType", Locale.getDefault().getLanguage()))) {
                    VideoWebViewActivity.webviewEntrance(MyInfoActivity.this, Constant.APP_VIDEO_TUTORIAL_EN, getString(R.string.MyVideos));
                } else {
                    VideoWebViewActivity.webviewEntrance(MyInfoActivity.this, Constant.APP_VIDEO_TUTORIAL, getString(R.string.MyVideos));
                }
            }
        });
        initUser();
    }

    private void initUser() {
        RoomAiWriterDatabase db = RoomAiWriterDatabase.getInstance(MyInfoActivity.this);
        List<UserEntity> list = db.userDao().getAll();
        if (list != null && list.size() > 0) {
            UserEntity user = list.get(0);

            Log.d(TAG, "user = " + user);
            if (user.getNickname() != null) {
                user_nickname.setText(user.getNickname());

            } else {
//                user_nickname.setText(user.getPhoneNum());
            }
            if (user.getUid() != null) {
                tv_userid.setText("ID:" + user.getUid());
            }
            showImage(user.getNickImage());
        }
        tv_version.setText(AppUtil.INSTANCE.getVersion());
    }

    private void showImage(String nickImage) {
        if (TextUtils.isEmpty(nickImage)) {
            return;
        }
        //利用Base64将字符串转换为ByteArrayInputStream
        byte[] byteArray = Base64.decode(nickImage, Base64.DEFAULT);
        if (byteArray.length == 0) {
        } else {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);

            //利用ByteArrayInputStream生成Bitmap
            Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
            user_imageView.setImageBitmap(bitmap);
        }
    }
}
